<!DOCTYPE HTML>
<html lang="en" >
    <!-- Start book Python数据分析课程讲义 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>Pandas的数据结构 | Python数据分析课程讲义</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="BigCat">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-tbfed-pagefooter/footer.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../../file/part03/3.2.html" />
    
    
    <link rel="prev" href="../../file/part03/3.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="3.1"
        data-chapter-title="Pandas的数据结构"
        data-filepath="file/part03/3.1.md"
        data-basepath="../.."
        data-revision="Thu Apr 27 2017 00:50:19 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        传智播客Python学院数据分析
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="file/part01/1.html">
            
                
                    <a href="../../file/part01/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        一、工作环境准备及数据分析建模理论基础
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="file/part01/1.1.html">
            
                
                    <a href="../../file/part01/1.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        Python 3.x新特性和编码回顾
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="file/part01/1.2.html">
            
                
                    <a href="../../file/part01/1.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        DIKW模型与数据工程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="file/part01/1.3.html">
            
                
                    <a href="../../file/part01/1.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        数据分析建模理论基础
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="file/part02/2.html">
            
                
                    <a href="../../file/part02/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        二、科学计算工具NumPy
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="file/part02/2.1.html">
            
                
                    <a href="../../file/part02/2.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        ndarray的创建与数据类型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="file/part02/2.2.html">
            
                
                    <a href="../../file/part02/2.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        ndarray的矩阵处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="file/part02/2.3.html">
            
                
                    <a href="../../file/part02/2.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        ndarray的元素处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="file/part02/2.4.html">
            
                
                    <a href="../../file/part02/2.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        实战案例：2016美国总统大选民意调查统计
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="file/part03/3.html">
            
                
                    <a href="../../file/part03/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        三、数据分析工具Pandas
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="3.1" data-path="file/part03/3.1.html">
            
                
                    <a href="../../file/part03/3.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        Pandas的数据结构
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="file/part03/3.2.html">
            
                
                    <a href="../../file/part03/3.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        Pandas的索引操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="file/part03/3.3.html">
            
                
                    <a href="../../file/part03/3.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        Pandas的对齐运算
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="file/part03/3.4.html">
            
                
                    <a href="../../file/part03/3.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        Pandas的函数应用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="file/part03/3.5.html">
            
                
                    <a href="../../file/part03/3.5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        Pandas的层级索引
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="file/part03/3.6.html">
            
                
                    <a href="../../file/part03/3.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        Pandas统计计算和描述
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="file/part03/3.7.html">
            
                
                    <a href="../../file/part03/3.7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.7.</b>
                        
                        Pandas分组与聚合
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="file/part03/3.8.html">
            
                
                    <a href="../../file/part03/3.8.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.8.</b>
                        
                        数据清洗、合并、转化和重构
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.9" data-path="file/part03/3.9.html">
            
                
                    <a href="../../file/part03/3.9.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.9.</b>
                        
                        聚类模型 -- K-Means介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.10" data-path="file/part03/3.10.html">
            
                
                    <a href="../../file/part03/3.10.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.10.</b>
                        
                        实战案例：全球食品数据分析
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="file/part04/4.html">
            
                
                    <a href="../../file/part04/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        四、数据可视化工具
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="file/part04/4.1.html">
            
                
                    <a href="../../file/part04/4.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        Matplotlib绘图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="file/part04/4.2.html">
            
                
                    <a href="../../file/part04/4.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        Seaborn绘图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="file/part04/4.3.html">
            
                
                    <a href="../../file/part04/4.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        Bokeh绘图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="file/part04/4.4.html">
            
                
                    <a href="../../file/part04/4.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        实战案例：世界高峰数据可视化
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="file/part06/6.html">
            
                
                    <a href="../../file/part06/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        五、自然语言处理NLTK
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="file/part06/6.1.html">
            
                
                    <a href="../../file/part06/6.1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        NLTK与自然语言处理基础
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="file/part06/6.2.html">
            
                
                    <a href="../../file/part06/6.2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        jieba分词
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="file/part06/6.3.html">
            
                
                    <a href="../../file/part06/6.3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        情感分析
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="file/part06/6.4.html">
            
                
                    <a href="../../file/part06/6.4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        文本相似度和分类
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="file/part06/6.6.html">
            
                
                    <a href="../../file/part06/6.6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        实战案例：微博情感分析
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../" >Python数据分析课程讲义</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="pandas&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;">Pandas&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;</h1>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> pandas <span class="hljs-keyword">as</span> pd
</code></pre>
<p>Pandas&#x6709;&#x4E24;&#x4E2A;&#x6700;&#x4E3B;&#x8981;&#x4E5F;&#x662F;&#x6700;&#x91CD;&#x8981;&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;&#xFF1A; <strong>Series</strong> &#x548C; <strong>DataFrame</strong></p>
<blockquote>
<h2 id="series">Series</h2>
</blockquote>
<p>Series&#x662F;&#x4E00;&#x79CD;&#x7C7B;&#x4F3C;&#x4E8E;&#x4E00;&#x7EF4;&#x6570;&#x7EC4;&#x7684; <strong>&#x5BF9;&#x8C61;</strong>&#xFF0C;&#x7531;&#x4E00;&#x7EC4;&#x6570;&#x636E;&#xFF08;&#x5404;&#x79CD;NumPy&#x6570;&#x636E;&#x7C7B;&#x578B;&#xFF09;&#x4EE5;&#x53CA;&#x4E00;&#x7EC4;&#x4E0E;&#x4E4B;&#x5BF9;&#x5E94;&#x7684;&#x7D22;&#x5F15;&#xFF08;&#x6570;&#x636E;&#x6807;&#x7B7E;&#xFF09;&#x7EC4;&#x6210;&#x3002;</p>
<ul>
<li>&#x7C7B;&#x4F3C;&#x4E00;&#x7EF4;&#x6570;&#x7EC4;&#x7684;&#x5BF9;&#x8C61;</li>
<li>&#x7531;&#x6570;&#x636E;&#x548C;&#x7D22;&#x5F15;&#x7EC4;&#x6210;<ul>
<li>&#x7D22;&#x5F15;(index)&#x5728;&#x5DE6;&#xFF0C;&#x6570;&#x636E;(values)&#x5728;&#x53F3;</li>
<li>&#x7D22;&#x5F15;&#x662F;&#x81EA;&#x52A8;&#x521B;&#x5EFA;&#x7684;</li>
</ul>
</li>
</ul>
<p><img src="../images/Series.png" alt=""></p>
<h4 id="1-&#x901A;&#x8FC7;list&#x6784;&#x5EFA;series">1. &#x901A;&#x8FC7;list&#x6784;&#x5EFA;Series</h4>
<blockquote>
<p>ser_obj = pd.Series(range(10))</p>
</blockquote>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x901A;&#x8FC7;list&#x6784;&#x5EFA;Series</span>
ser_obj = pd.Series(range(<span class="hljs-number">10</span>, <span class="hljs-number">20</span>))
print(ser_obj.head(<span class="hljs-number">3</span>))

print(ser_obj)

print(type(ser_obj))
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python">0    10
1    11
2    12
dtype: int64

0    10
1    11
2    12
3    13
4    14
5    15
6    16
7    17
8    18
9    19
dtype: int64

&lt;class &apos;pandas.core.series.Series&apos;&gt;
</code></pre>
<h4 id="2-&#x83B7;&#x53D6;&#x6570;&#x636E;&#x548C;&#x7D22;&#x5F15;">2. &#x83B7;&#x53D6;&#x6570;&#x636E;&#x548C;&#x7D22;&#x5F15;</h4>
<blockquote>
<p>ser_obj.index &#x548C; ser_obj.values</p>
</blockquote>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x83B7;&#x53D6;&#x6570;&#x636E;</span>
print(ser_obj.values)

<span class="hljs-comment"># &#x83B7;&#x53D6;&#x7D22;&#x5F15;</span>
print(ser_obj.index)
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python">[<span class="hljs-number">10</span> <span class="hljs-number">11</span> <span class="hljs-number">12</span> <span class="hljs-number">13</span> <span class="hljs-number">14</span> <span class="hljs-number">15</span> <span class="hljs-number">16</span> <span class="hljs-number">17</span> <span class="hljs-number">18</span> <span class="hljs-number">19</span>]
RangeIndex(start=<span class="hljs-number">0</span>, stop=<span class="hljs-number">10</span>, step=<span class="hljs-number">1</span>)
</code></pre>
<h4 id="3-&#x901A;&#x8FC7;&#x7D22;&#x5F15;&#x83B7;&#x53D6;&#x6570;&#x636E;">3. &#x901A;&#x8FC7;&#x7D22;&#x5F15;&#x83B7;&#x53D6;&#x6570;&#x636E;</h4>
<blockquote>
<p>ser_obj[idx]</p>
</blockquote>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment">#&#x901A;&#x8FC7;&#x7D22;&#x5F15;&#x83B7;&#x53D6;&#x6570;&#x636E;</span>
print(ser_obj[<span class="hljs-number">0</span>])
print(ser_obj[<span class="hljs-number">8</span>])
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-number">10</span>
<span class="hljs-number">18</span>
</code></pre>
<h4 id="4-&#x7D22;&#x5F15;&#x4E0E;&#x6570;&#x636E;&#x7684;&#x5BF9;&#x5E94;&#x5173;&#x7CFB;&#x4E0D;&#x88AB;&#x8FD0;&#x7B97;&#x7ED3;&#x679C;&#x5F71;&#x54CD;">4. &#x7D22;&#x5F15;&#x4E0E;&#x6570;&#x636E;&#x7684;&#x5BF9;&#x5E94;&#x5173;&#x7CFB;&#x4E0D;&#x88AB;&#x8FD0;&#x7B97;&#x7ED3;&#x679C;&#x5F71;&#x54CD;</h4>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x7D22;&#x5F15;&#x4E0E;&#x6570;&#x636E;&#x7684;&#x5BF9;&#x5E94;&#x5173;&#x7CFB;&#x4E0D;&#x88AB;&#x8FD0;&#x7B97;&#x7ED3;&#x679C;&#x5F71;&#x54CD;</span>
print(ser_obj * <span class="hljs-number">2</span>)
print(ser_obj &gt; <span class="hljs-number">15</span>)
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-number">0</span>    <span class="hljs-number">20</span>
<span class="hljs-number">1</span>    <span class="hljs-number">22</span>
<span class="hljs-number">2</span>    <span class="hljs-number">24</span>
<span class="hljs-number">3</span>    <span class="hljs-number">26</span>
<span class="hljs-number">4</span>    <span class="hljs-number">28</span>
<span class="hljs-number">5</span>    <span class="hljs-number">30</span>
<span class="hljs-number">6</span>    <span class="hljs-number">32</span>
<span class="hljs-number">7</span>    <span class="hljs-number">34</span>
<span class="hljs-number">8</span>    <span class="hljs-number">36</span>
<span class="hljs-number">9</span>    <span class="hljs-number">38</span>
dtype: int64

<span class="hljs-number">0</span>    <span class="hljs-keyword">False</span>
<span class="hljs-number">1</span>    <span class="hljs-keyword">False</span>
<span class="hljs-number">2</span>    <span class="hljs-keyword">False</span>
<span class="hljs-number">3</span>    <span class="hljs-keyword">False</span>
<span class="hljs-number">4</span>    <span class="hljs-keyword">False</span>
<span class="hljs-number">5</span>    <span class="hljs-keyword">False</span>
<span class="hljs-number">6</span>     <span class="hljs-keyword">True</span>
<span class="hljs-number">7</span>     <span class="hljs-keyword">True</span>
<span class="hljs-number">8</span>     <span class="hljs-keyword">True</span>
<span class="hljs-number">9</span>     <span class="hljs-keyword">True</span>
dtype: bool
</code></pre>
<h4 id="5-&#x901A;&#x8FC7;dict&#x6784;&#x5EFA;series">5. &#x901A;&#x8FC7;dict&#x6784;&#x5EFA;Series</h4>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x901A;&#x8FC7;dict&#x6784;&#x5EFA;Series</span>
year_data = {<span class="hljs-number">2001</span>: <span class="hljs-number">17.8</span>, <span class="hljs-number">2002</span>: <span class="hljs-number">20.1</span>, <span class="hljs-number">2003</span>: <span class="hljs-number">16.5</span>}
ser_obj2 = pd.Series(year_data)
print(ser_obj2.head())
print(ser_obj2.index)
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-number">2001</span>    <span class="hljs-number">17.8</span>
<span class="hljs-number">2002</span>    <span class="hljs-number">20.1</span>
<span class="hljs-number">2003</span>    <span class="hljs-number">16.5</span>
dtype: float64
Int64Index([<span class="hljs-number">2001</span>, <span class="hljs-number">2002</span>, <span class="hljs-number">2003</span>], dtype=<span class="hljs-string">&apos;int64&apos;</span>)
</code></pre>
<h4 id="name&#x5C5E;&#x6027;">name&#x5C5E;&#x6027;</h4>
<blockquote>
<p>&#x5BF9;&#x8C61;&#x540D;&#xFF1A;ser_obj.name</p>
<p>&#x5BF9;&#x8C61;&#x7D22;&#x5F15;&#x540D;&#xFF1A;ser_obj.index.name</p>
</blockquote>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># name&#x5C5E;&#x6027;</span>
ser_obj2.name = <span class="hljs-string">&apos;temp&apos;</span>
ser_obj2.index.name = <span class="hljs-string">&apos;year&apos;</span>
print(ser_obj2.head())
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python">year
<span class="hljs-number">2001</span>    <span class="hljs-number">17.8</span>
<span class="hljs-number">2002</span>    <span class="hljs-number">20.1</span>
<span class="hljs-number">2003</span>    <span class="hljs-number">16.5</span>
Name: temp, dtype: float64
</code></pre>
<hr>
<hr>
<blockquote>
<h2 id="dataframe">DataFrame</h2>
</blockquote>
<p>DataFrame&#x662F;&#x4E00;&#x4E2A;&#x8868;&#x683C;&#x578B;&#x7684;&#x6570;&#x636E;&#x7ED3;&#x6784;&#xFF0C;&#x5B83;&#x542B;&#x6709;&#x4E00;&#x7EC4;&#x6709;&#x5E8F;&#x7684;&#x5217;&#xFF0C;&#x6BCF;&#x5217;&#x53EF;&#x4EE5;&#x662F;&#x4E0D;&#x540C;&#x7C7B;&#x578B;&#x7684;&#x503C;&#x3002;DataFrame&#x65E2;&#x6709;&#x884C;&#x7D22;&#x5F15;&#x4E5F;&#x6709;&#x5217;&#x7D22;&#x5F15;&#xFF0C;&#x5B83;&#x53EF;&#x4EE5;&#x88AB;&#x770B;&#x505A;&#x662F;&#x7531;Series&#x7EC4;&#x6210;&#x7684;&#x5B57;&#x5178;&#xFF08;&#x5171;&#x7528;&#x540C;&#x4E00;&#x4E2A;&#x7D22;&#x5F15;&#xFF09;&#xFF0C;&#x6570;&#x636E;&#x662F;&#x4EE5;&#x4E8C;&#x7EF4;&#x7ED3;&#x6784;&#x5B58;&#x653E;&#x7684;&#x3002;</p>
<ul>
<li>&#x7C7B;&#x4F3C;&#x591A;&#x7EF4;&#x6570;&#x7EC4;/&#x8868;&#x683C;&#x6570;&#x636E; (&#x5982;&#xFF0C;excel, R&#x4E2D;&#x7684;data.frame)</li>
<li>&#x6BCF;&#x5217;&#x6570;&#x636E;&#x53EF;&#x4EE5;&#x662F;&#x4E0D;&#x540C;&#x7684;&#x7C7B;&#x578B;</li>
<li>&#x7D22;&#x5F15;&#x5305;&#x62EC;&#x5217;&#x7D22;&#x5F15;&#x548C;&#x884C;&#x7D22;&#x5F15;</li>
</ul>
<p><img src="../images/DataFrame.png" alt=""></p>
<h4 id="1-&#x901A;&#x8FC7;ndarray&#x6784;&#x5EFA;dataframe">1. &#x901A;&#x8FC7;ndarray&#x6784;&#x5EFA;DataFrame</h4>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np

<span class="hljs-comment"># &#x901A;&#x8FC7;ndarray&#x6784;&#x5EFA;DataFrame</span>
array = np.random.randn(<span class="hljs-number">5</span>,<span class="hljs-number">4</span>)
print(array)

df_obj = pd.DataFrame(array)
print(df_obj.head())
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python">[[ <span class="hljs-number">0.83500594</span> -<span class="hljs-number">1.49290138</span> -<span class="hljs-number">0.53120106</span> -<span class="hljs-number">0.11313932</span>]
 [ <span class="hljs-number">0.64629762</span> -<span class="hljs-number">0.36779941</span>  <span class="hljs-number">0.08011084</span>  <span class="hljs-number">0.60080495</span>]
 [-<span class="hljs-number">1.23458522</span>  <span class="hljs-number">0.33409674</span> -<span class="hljs-number">0.58778195</span> -<span class="hljs-number">0.73610573</span>]
 [-<span class="hljs-number">1.47651414</span>  <span class="hljs-number">0.99400187</span>  <span class="hljs-number">0.21001995</span> -<span class="hljs-number">0.90515656</span>]
 [ <span class="hljs-number">0.56669419</span>  <span class="hljs-number">1.38238348</span> -<span class="hljs-number">0.49099007</span>  <span class="hljs-number">1.94484598</span>]]

          <span class="hljs-number">0</span>         <span class="hljs-number">1</span>         <span class="hljs-number">2</span>         <span class="hljs-number">3</span>
<span class="hljs-number">0</span>  <span class="hljs-number">0.835006</span> -<span class="hljs-number">1.492901</span> -<span class="hljs-number">0.531201</span> -<span class="hljs-number">0.113139</span>
<span class="hljs-number">1</span>  <span class="hljs-number">0.646298</span> -<span class="hljs-number">0.367799</span>  <span class="hljs-number">0.080111</span>  <span class="hljs-number">0.600805</span>
<span class="hljs-number">2</span> -<span class="hljs-number">1.234585</span>  <span class="hljs-number">0.334097</span> -<span class="hljs-number">0.587782</span> -<span class="hljs-number">0.736106</span>
<span class="hljs-number">3</span> -<span class="hljs-number">1.476514</span>  <span class="hljs-number">0.994002</span>  <span class="hljs-number">0.210020</span> -<span class="hljs-number">0.905157</span>
<span class="hljs-number">4</span>  <span class="hljs-number">0.566694</span>  <span class="hljs-number">1.382383</span> -<span class="hljs-number">0.490990</span>  <span class="hljs-number">1.944846</span>
</code></pre>
<h4 id="2-&#x901A;&#x8FC7;dict&#x6784;&#x5EFA;dataframe">2. &#x901A;&#x8FC7;dict&#x6784;&#x5EFA;DataFrame</h4>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x901A;&#x8FC7;dict&#x6784;&#x5EFA;DataFrame</span>
dict_data = {<span class="hljs-string">&apos;A&apos;</span>: <span class="hljs-number">1</span>, 
             <span class="hljs-string">&apos;B&apos;</span>: pd.Timestamp(<span class="hljs-string">&apos;20170426&apos;</span>),
             <span class="hljs-string">&apos;C&apos;</span>: pd.Series(<span class="hljs-number">1</span>, index=list(range(<span class="hljs-number">4</span>)),dtype=<span class="hljs-string">&apos;float32&apos;</span>),
             <span class="hljs-string">&apos;D&apos;</span>: np.array([<span class="hljs-number">3</span>] * <span class="hljs-number">4</span>,dtype=<span class="hljs-string">&apos;int32&apos;</span>),
             <span class="hljs-string">&apos;E&apos;</span>: [<span class="hljs-string">&quot;Python&quot;</span>,<span class="hljs-string">&quot;Java&quot;</span>,<span class="hljs-string">&quot;C++&quot;</span>,<span class="hljs-string">&quot;C&quot;</span>],
             <span class="hljs-string">&apos;F&apos;</span>: <span class="hljs-string">&apos;ITCast&apos;</span> }
<span class="hljs-comment">#print dict_data</span>
df_obj2 = pd.DataFrame(dict_data)
print(df_obj2)
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python">   A          B    C  D       E       F
<span class="hljs-number">0</span>  <span class="hljs-number">1</span> <span class="hljs-number">2017</span>-<span class="hljs-number">04</span>-<span class="hljs-number">26</span>  <span class="hljs-number">1.0</span>  <span class="hljs-number">3</span>  Python  ITCast
<span class="hljs-number">1</span>  <span class="hljs-number">1</span> <span class="hljs-number">2017</span>-<span class="hljs-number">04</span>-<span class="hljs-number">26</span>  <span class="hljs-number">1.0</span>  <span class="hljs-number">3</span>    Java  ITCast
<span class="hljs-number">2</span>  <span class="hljs-number">1</span> <span class="hljs-number">2017</span>-<span class="hljs-number">04</span>-<span class="hljs-number">26</span>  <span class="hljs-number">1.0</span>  <span class="hljs-number">3</span>     C++  ITCast
<span class="hljs-number">3</span>  <span class="hljs-number">1</span> <span class="hljs-number">2017</span>-<span class="hljs-number">04</span>-<span class="hljs-number">26</span>  <span class="hljs-number">1.0</span>  <span class="hljs-number">3</span>       C  ITCast
</code></pre>
<h4 id="3-&#x901A;&#x8FC7;&#x5217;&#x7D22;&#x5F15;&#x83B7;&#x53D6;&#x5217;&#x6570;&#x636E;&#xFF08;series&#x7C7B;&#x578B;&#xFF09;">3. &#x901A;&#x8FC7;&#x5217;&#x7D22;&#x5F15;&#x83B7;&#x53D6;&#x5217;&#x6570;&#x636E;&#xFF08;Series&#x7C7B;&#x578B;&#xFF09;</h4>
<blockquote>
<p>df_obj[col_idx] &#x6216; df_obj.col_idx</p>
</blockquote>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x901A;&#x8FC7;&#x5217;&#x7D22;&#x5F15;&#x83B7;&#x53D6;&#x5217;&#x6570;&#x636E;</span>
print(df_obj2[<span class="hljs-string">&apos;A&apos;</span>])
print(type(df_obj2[<span class="hljs-string">&apos;A&apos;</span>]))

print(df_obj2.A)
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python">0    1.0
1    1.0
2    1.0
3    1.0
Name: A, dtype: float64
&lt;class &apos;pandas.core.series.Series&apos;&gt;
0    1.0
1    1.0
2    1.0
3    1.0
Name: A, dtype: float64
</code></pre>
<h4 id="4-&#x589E;&#x52A0;&#x5217;&#x6570;&#x636E;">4. &#x589E;&#x52A0;&#x5217;&#x6570;&#x636E;</h4>
<blockquote>
<p>df_obj[new_col_idx] = data</p>
<p>&#x7C7B;&#x4F3C;Python&#x7684; dict&#x6DFB;&#x52A0;key-value</p>
</blockquote>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x589E;&#x52A0;&#x5217;</span>
df_obj2[<span class="hljs-string">&apos;G&apos;</span>] = df_obj2[<span class="hljs-string">&apos;D&apos;</span>] + <span class="hljs-number">4</span>
print(df_obj2.head())
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python">     A          B    C  D       E       F  G
<span class="hljs-number">0</span>  <span class="hljs-number">1.0</span> <span class="hljs-number">2017</span>-<span class="hljs-number">01</span>-<span class="hljs-number">02</span>  <span class="hljs-number">1.0</span>  <span class="hljs-number">3</span>  Python  ITCast  <span class="hljs-number">7</span>
<span class="hljs-number">1</span>  <span class="hljs-number">1.0</span> <span class="hljs-number">2017</span>-<span class="hljs-number">01</span>-<span class="hljs-number">02</span>  <span class="hljs-number">1.0</span>  <span class="hljs-number">3</span>    Java  ITCast  <span class="hljs-number">7</span>
<span class="hljs-number">2</span>  <span class="hljs-number">1.0</span> <span class="hljs-number">2017</span>-<span class="hljs-number">01</span>-<span class="hljs-number">02</span>  <span class="hljs-number">1.0</span>  <span class="hljs-number">3</span>     C++  ITCast  <span class="hljs-number">7</span>
<span class="hljs-number">3</span>  <span class="hljs-number">1.0</span> <span class="hljs-number">2017</span>-<span class="hljs-number">01</span>-<span class="hljs-number">02</span>  <span class="hljs-number">1.0</span>  <span class="hljs-number">3</span>       C  ITCast  <span class="hljs-number">7</span>
</code></pre>
<h4 id="5-&#x5220;&#x9664;&#x5217;">5. &#x5220;&#x9664;&#x5217;</h4>
<blockquote>
<p>del df_obj[col_idx]</p>
</blockquote>
<p>&#x793A;&#x4F8B;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># &#x5220;&#x9664;&#x5217;</span>
<span class="hljs-keyword">del</span>(df_obj2[<span class="hljs-string">&apos;G&apos;</span>] )
print(df_obj2.head())
</code></pre>
<p>&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-python">     A          B    C  D       E       F
<span class="hljs-number">0</span>  <span class="hljs-number">1.0</span> <span class="hljs-number">2017</span>-<span class="hljs-number">01</span>-<span class="hljs-number">02</span>  <span class="hljs-number">1.0</span>  <span class="hljs-number">3</span>  Python  ITCast
<span class="hljs-number">1</span>  <span class="hljs-number">1.0</span> <span class="hljs-number">2017</span>-<span class="hljs-number">01</span>-<span class="hljs-number">02</span>  <span class="hljs-number">1.0</span>  <span class="hljs-number">3</span>    Java  ITCast
<span class="hljs-number">2</span>  <span class="hljs-number">1.0</span> <span class="hljs-number">2017</span>-<span class="hljs-number">01</span>-<span class="hljs-number">02</span>  <span class="hljs-number">1.0</span>  <span class="hljs-number">3</span>     C++  ITCast
<span class="hljs-number">3</span>  <span class="hljs-number">1.0</span> <span class="hljs-number">2017</span>-<span class="hljs-number">01</span>-<span class="hljs-number">02</span>  <span class="hljs-number">1.0</span>  <span class="hljs-number">3</span>       C  ITCast
</code></pre>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; BigCat all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x300C;Revision Time:
2017-04-25 23:02:59&#x300D;
</span></footer>
                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../../file/part03/3.html" class="navigation navigation-prev " aria-label="Previous page: 三、数据分析工具Pandas"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../../file/part03/3.2.html" class="navigation navigation-next " aria-label="Next page: Pandas的索引操作"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../../gitbook/app.js"></script>

    
    <script src="../../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

    
    <script src="../../gitbook/plugins/gitbook-plugin-livereload/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"disqus":{"shortName":"gitbookuse"},"github":{"url":"https://github.com/dododream"},"search-pro":{"cutWordLib":"nodejieba","defineWord":["gitbook-use"]},"sharing":{"weibo":true,"facebook":true,"twitter":true,"google":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"tbfed-pagefooter":{"copyright":"Copyright © BigCat","modify_label":"「Revision Time:","modify_format":"YYYY-MM-DD HH:mm:ss」"},"baidu":{"token":"ff100361cdce95dd4c8fb96b4009f7bc"},"sitemap":{"hostname":"http://www.treenewbee.top"},"donate":{"wechat":"http://weixin.png","alipay":"http://alipay.png","title":"","button":"赏","alipayText":"支付宝打赏","wechatText":"微信打赏"},"edit-link":{"base":"https://github.com/dododream/edit","label":"Edit This Page"},"splitter":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2},"livereload":{}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book Python数据分析课程讲义 -->
</html>
